<?php
/**
 * Created by JetBrains PhpStorm.
 * User: cgp
 * Date: 13-11-1
 * Time: 下午12:11
 * 票付通票类处理
 */

namespace pft\Prod;
use pft\Member\MemberAccount;

class Ticket {
    private $db;
    public function __construct($db)
    {
        $this->db = $db;
    }

    /**
     * 添加票类时向pft_product_sale_list表写入数据，分配购买权限
     *
     * @param $apply_id     供应商ID
     * @return int
     */
    public function AddProd2SaleList($apply_id)
    {
        //获取供应商的产品ID
        $sql_get_pid = "SELECT id FROM uu_products WHERE apply_did=$apply_id";
        $this->db->query($sql_get_pid);
        $pids = array();
        while($ps = $this->db->fetch_assoc()) {
            $pids[] = $ps['id'];
        }
        if(count($pids)===0) {
            return 1;
        }
        $pids_str = implode(',', $pids);
        //查找出可以销售该供应商所有产品的分销商
        $sql = "SELECT id FROM pft_product_sale_list "
            ."WHERE aid={$apply_id} AND pids='A'";
        $this->db->query($sql);
        $list = array();
        while($r=$this->db->fetch_assoc()) {
            $list[] = $r['id'];
        }
        if(count($list)===0) {
            return 1;
        }
        $sale_ids = implode(',', $list);
        //更新可以销售该供应商所有产品的分销商的产品ID
        $sql_update_sale = "UPDATE pft_product_sale_list SET pids='{$pids_str}'"
            ." WHERE id IN($sale_ids)";
        $this->db->query($sql_update_sale);
        return 1;
    }
    /**
     * 获取7天内变动的产品
     * @param $dtype
     * @param $did
     * @param $ctype
     * @param null $bt
     * @param null $et
     * @return array
     */
    public function ProductChanged($dtype, $did, $ctype, $bt=null, $et=null)
    {
        $bt = !$bt ? date('Y-m-d',strtotime('-7 days')) : $bt;
        $et = !$et ? date('Y-m-d') : $et;
        if($dtype==MemberAccount::APPLYER) {
            $sql = "SELECT distinct(v.pid) ,v.ctype,".
                "date_format(v.created_at,'%Y%m%d') as ct from ";
            $sql .= "uu_prod_verify v where v.did=$did";
            $sql .= !$ctype ? " " : " and v.ctype=$ctype";
            $sql .= " and v.created_at between('{$bt} 00:00:00') "
                ."and ('{$et} 23:59:59')";
        }
        elseif($dtype==MemberAccount::BUYER) {
            $sql = <<<SQL
SELECT distinct(v.pid) ,v.ctype,s.aid, date_format(v.created_at,'%Y%m%d') as ct
from pft_product_sale_list s
LEFT JOIN uu_prod_verify v ON v.did=s.aid where s.fid=$did
AND v.created_at between('{$bt} 00:00:00') and ('{$et} 23:59:59')
SQL;
        }
//        if($did==94){
//            echo $sql;
//        }
        $this->db->query($sql);
        $prod_list = array();
        $pid_list = array();
        while($rows = $this->db->fetch_assoc()) {
            //$ct = strtotime($rows['ct']);
            $prod_list[$rows['ct']][$rows['ctype']] += 1;
            $pid_list[] = $rows['pid'];
        }
        if(!$ctype) {
//            if($did==94) {
//                var_export($prod_list);
//            }
            return $prod_list;
        }
        $prod_list2 = array();
        if(!count($pid_list)) {
            return array();
        }
        $pid = implode(',', $pid_list);
        $str2 = "select t.title as t_title,l.p_type,l.title as p_title,t.landid"
            ." from uu_jq_ticket t,uu_land l ";
        $str2 .= " where t.pid in($pid) AND l.id = t.landid";
        //echo $str2;
        $this->db->query($str2);
        while($rows=$this->db->fetch_assoc($str2)) {
            $prod_list2[] = array(
                'i' => $rows['landid'],
                't' => $rows['t_title'],
                'p' => $rows['p_title'],
                'e' => $rows['p_type']
            );
        }
        return $prod_list2;
    }

    /**
     * 根据产品ID获取票类
     * @param $landid
     * @param $did
     * @param int $status
     * @param int $fpriceFlag
     * @param \SoapClient $soap
     * @return array
     */
    public function GetTicketByLimit($landid, $did, $status=0, $fpriceFlag=1,
                                     \SoapClient $soap)
    {
        $params = array(
            'landid' =>$landid,
            'status'=> $status,
            'apply_limit' => 1,
            'limit' =>0,
            'offset'=>10
        );
        $output = array();
        $prod = new Product($this->db);
        $lists = $prod->GetAllTicketList($params);
        foreach($lists as $list) {
            $fprice = $soap->get_Dynamic_Price_Merge($list['pid'],date("Y-m-d"),2, '', '', 0);//供应价
            $uprice = $soap->get_Dynamic_Price_Merge($list['pid'],date("Y-m-d"),2, '', '', 1);//零售价
            $fprice = $fprice>=0 ? $fprice/100 :'未配';
            $uprice = $uprice>=0 ? $uprice/100 :'未配';
            if($fpriceFlag) {
                $sql = "SELECT s.tid, s.dprice FROM uu_priceset s LEFT JOIN uu_products p ON p.id=s.tid ";
                $sql .= "WHERE s.tid={$list['pid']} AND s.pid={$did} AND (p.p_status=0 OR p.p_status=3) AND s.aid=0 LIMIT 1";
                $GLOBALS['le']->query($sql);
                $GLOBALS['le']->fetch_assoc();
                if(!$GLOBALS['le']->f('tid')) {
                    continue;
                }
                $fprice += $GLOBALS['le']->f('dprice')/100;
            }
            $output[] = array(
                'ticketTitle'=>$list['ticket_title'],
                'marketPrice'=>$list['tprice'],
                'salePrice'=>$uprice,
                'setlPrice'=>$fprice,
                'validDays'=> !$list['delaydays']? '当天有效' : $list['delaydays'].'天',
                'num'=>!$list['buy_limit_low']? '不限' :$list['buy_limit_low'].'张起订' ,
                'preOrderDays'=>$list['ddays']==0 ? '可当天预定' : $list['ddays'],
                'prodid'=>$list['pid'],
                'landid'=>$list['landid'],
                'status'=>$list['p_status']
            );
        }
        return $output;
    }

    public function CountTicketSale($tid, $soap)
    {
        $aid = $_SESSION['sid']==1?'':$_SESSION['sid'];
        $res = $soap->Order_Globle_Search("","","",$tid,"","","","","","","","",
            "","","","","","",1,1,0,999,2,"","","","","","","",$aid);
        return $res;
//            $stype,$payinfo,$pmode,$remotenum,$origin,
//            $p_type,$order_confirm,$aid,$concat,$ifpack);
    }
}